package com.czx.controller;

import com.czx.entity.Message;
import com.czx.entity.MyPage;
import com.czx.common.security.UserTokenThreadLocal;
import com.czx.entity.User;
import com.czx.service.LetterService;
import com.czx.service.SysMessageService;
import com.czx.service.UserService;
import com.czx.vo.UserToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class LetterController {
    @Autowired
    private LetterService letterService;
    @Autowired
    private SysMessageService sysMessageService;
    @Autowired
    private UserService userService;

    @RequestMapping("/letter")
    public String getLetter(MyPage page, Model model){
        UserToken userToken = UserTokenThreadLocal.get();
        //通过id查询所有的未读私信
        Integer letterUnreadCount = letterService.secMessage(userToken.getUser().getId());
        model.addAttribute("letterUnreadCount",letterUnreadCount);
        //查询所有未读私信
        Integer id = UserTokenThreadLocal.get().getUser().getId();
        MyPage myPage= letterService.getLetter(page,id);
        model.addAttribute("page",myPage);

        return "site/letter";
    }
    @RequestMapping("/letterList")
    public String getLetterList(MyPage page, Model model, Integer fromId){
        User user = userService.getUserById(fromId);
        UserToken userToken = UserTokenThreadLocal.get();
        Integer toId=userToken.getUser().getId();
       MyPage myPage= letterService.getLetterList(page,fromId,toId);
       model.addAttribute("page",myPage);
       model.addAttribute("user",user);
       return "site/letter-detail";

    }
}
